﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace SuperMoney.AdPlatformCallback.Controllers
{
	public class LiMeiController : Controller
	{
		const string adPlatform = "limei";
		private static readonly string ios_secret = ConfigurationManager.AppSettings[adPlatform + "-ios-secret"];
		private static readonly int ios_ratio = Int32.Parse(ConfigurationManager.AppSettings[adPlatform + "-ios-ratio"]);
		private static readonly string android_secret = ConfigurationManager.AppSettings[adPlatform + "-android-secret"];
		private static readonly int android_ratio = Int32.Parse(ConfigurationManager.AppSettings[adPlatform + "-android-ratio"]);

		//
		// GET: /LiMei/Android

		public ActionResult Android(string aduid, string uid, string aid, int point, string source, int timestamp, string sign)
		{
			Dictionary<string, object> dict = new Dictionary<string, object>();
			foreach (var key in Request.QueryString.AllKeys)
			{
				if (key == "sign") continue;
				dict.Add(key, Request.QueryString[key]);
			}
			dict = dict.OrderBy(p => p.Key).ToDictionary(p => p.Key, p => p.Value);

			string str = null;
			foreach (var kv in dict)
			{
				string kvStr = String.Format("{0}{1}", kv.Key, kv.Value);
				if (str == null)
				{
					str = kvStr;
				}
				else
				{
					str += kvStr;
				}
			}
			HMACSHA1 hmacsha1 = new HMACSHA1(Encoding.ASCII.GetBytes(android_secret));
			string strToSign = Convert.ToBase64String(hmacsha1.ComputeHash(Encoding.ASCII.GetBytes(str)));

			if (strToSign == sign)
			{
				string _AdPlatform = adPlatform;
				string _AdOrderId = aduid;
				DateTime _AdOrderTime = new DateTime(1970, 1, 1).AddSeconds(timestamp);
				string _AdDeviceId = uid;
				string _AdId = "";
				string _AdName = "";
				int _Integral = point;
				decimal _AppIncode = point / android_ratio;


				//AdRecordService.Instance.Create(_AdPlatform, _AdOrderId, _AdOrderTime, 0, aid, "", "", null, _Integral, _AppIncode, app_id, "android", aid);
			}
			return null;
		}

		//
		// GET: /LiMei/iOS

		public ActionResult iOS(string snuid, string device_id, string app_id, string currency, string app_ratio, string time_stamp, string token, string ad_name, string pack_name)
		{
			return null;
		}
	}
}
